package cn.android.support.v7.lib.sin.crown.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.model.Single;
import cn.android.support.v7.lib.sin.crown.utils.AssetsUtils;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

/**
 * 是否选中控件。如自动登录旁的那个打钩钩小控件
 * Created by 彭治铭 on 2017/9/16.
 */

public class SingleChoose extends View {
    public SingleChoose(Context context) {
        super(context);
        init();
    }

    public SingleChoose(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private BaseListenner<SingleChoose> onStateChangeListener = null;

    //状态监听
    public void setOnStateChangeListener(BaseListenner<SingleChoose> onStateChangeListener) {
        this.onStateChangeListener = onStateChangeListener;
    }

    //实体类
    private Single single;

    public Single getSingle() {
        return single;
    }

    public void setSingle(Single single) {
        this.single = single;
        invalidate();
    }

    //状态
    public boolean isState() {
        return single.isState();
    }

    public void setState(boolean state) {
        if (single.isState() == state) {
            return;
        }
        invalidate();
        single.setState(state);
        if (onStateChangeListener != null) {
            onStateChangeListener.onResult(this);
        }
    }

    int textSize;//文本大小
    int startDistance;//文本开始间隙

    //文本信息
    public String getMession() {
        return single.getMession();
    }

    public void setMession(String mession) {
        if (single.getMession().equals(mession)) {
            return;
        }
        single.setMession(mession);
        invalidate();
    }

    public int getTextSize() {
        return textSize;
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
        paint.setTextSize(textSize);
        invalidate();
    }

    public void setTextColor(int color) {
        paint.setColor(color);
    }

    public int getStartDistance() {
        return startDistance;
    }

    //设置文字与图片的间隙
    public void setStartDistance(int startDistance) {
        this.startDistance = startDistance;
        invalidate();
    }

    //位图,常态,选中
    Bitmap general, selected;

    //设置自定义位图，需要自己对位图进行适配。内部不会再适配
    public void setBitmap(Bitmap general, Bitmap selected) {
        this.general = general;
        this.selected = selected;
        compute();
        requestLayout();
    }

    private void init() {
        //初始化数据实体类
        single = new Single("0", 0, "点我");

        general = AssetsUtils.getInstance().getBitmapFromAssets( "crown/selector/general.png", 0, true);
        selected = AssetsUtils.getInstance().getBitmapFromAssets( "crown/selector/selected.png", 0, true);
        general = ProportionUtils.getInstance().adapterBitmap(general);
        selected = ProportionUtils.getInstance().adapterBitmap(selected);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setTextAlign(Paint.Align.LEFT);
        paint.setColor(Color.BLACK);
        setLayerType(View.LAYER_TYPE_HARDWARE, paint);

        compute();
    }

    //计算宽和高等常量
    public void compute() {
        width = selected.getWidth() > general.getWidth() ? selected.getWidth() : general.getWidth();
        ;
        height = selected.getHeight() > general.getHeight() ? selected.getHeight() : general.getHeight();
        textSize = (int) (height);
        startDistance = textSize / 2;
        paint.setTextSize(textSize);
        width = (int) (paint.measureText(single.getMession(), 0, single.getMession().length()) + startDistance + width);
    }

    public int width, height;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(width, (int) (height * 2));
    }

    Paint paint;

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        int y = (getHeight() - general.getHeight()) / 2;
        if (single.isState()) {
            canvas.drawBitmap(selected, 0, y, paint);
        } else {
            canvas.drawBitmap(general, 0, y, paint);
        }
        int x = selected.getWidth();
        x += startDistance;
        Rect rect = new Rect();
        paint.getTextBounds(single.getMession(), 0, single.getMession().length(), rect);
        y += rect.height() * 0.9;
        canvas.drawText(single.getMession(), x, y, paint);
    }

    //点击是否具备取消能力，true点击能够取消，false 点击不能取消。默认不能取消
    public boolean cancel = false;
    public boolean touchEvent=true;//是否具备触摸事件。默认具备
    public void setCancel(boolean cancel) {
        this.cancel = cancel;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (touchEvent&&event.getAction() == MotionEvent.ACTION_DOWN && (!single.isState() || cancel)) {
            setState(!single.isState());
        }
        return super.onTouchEvent(event);
    }

    public void setTouchEvent(boolean touchEvent) {
        this.touchEvent = touchEvent;
    }
}
